import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login'},
  { path: '/login', component: ()=>import(/* webpackChunkName: "Login" */ '@/components/login') },
  { path: '/welcome' ,redirect: '/users', component: ()=>import(/* webpackChunkName: "Welcome" */ '@/components/welcome'),children:[
    {path: '/users',component:()=>import(/* webpackChunkName: "User" */ '@/components/user/users')},
    {path: '/roles',component:()=>import(/* webpackChunkName: "Permissions" */ '@/components/Permissions/roles')},
    {path: '/rights',component:()=>import(/* webpackChunkName: "Permissions" */ '@/components/Permissions/rights')},
    {path: '/goods',component:()=>import(/* webpackChunkName: "Shop" */ '@/components/shop/goods')},
    {path: '/params',component:()=>import(/* webpackChunkName: "Shop" */ '@/components/shop/params')},
    {path: '/categories',component:()=>import(/* webpackChunkName: "Shop" */ '@/components/shop/categories')},
    {path: '/orders',component:()=>import(/* webpackChunkName: "Orde" */ '@/components/orde/orders')},
    {path: '/reports',component:()=>import(/* webpackChunkName: "Reports" */ '@/components/reports/reports')},
    {path: '/goods/add',component:()=>import(/* webpackChunkName: "Shop" */ '@/components/shop/add')},
  ]},
]

const router = new VueRouter({
  mode: 'hash',
  routes
})
router.beforeEach((to, from, next) => {
  if(to.path !== '/login') {
    var token = window.sessionStorage.getItem('token');
    if(token){
      next()
    }else {
      next({path:'/login'})
    }
  }
  next();
})
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
